import { get, post, put, del, upload } from '@/utils/request'
import type { 
  ProjectRequest, 
  ProjectResponse, 
  ApplicationRequest, 
  ApplicationResponse,
  QueryParams,
  PaginatedResponse
} from '@/types/api'

// 项目相关API
export const projectApi = {
  // 获取项目列表
  getProjects: (params?: QueryParams): Promise<PaginatedResponse<ProjectResponse['data']>> => {
    return get<PaginatedResponse<ProjectResponse['data']>>('/projects', params)
  },

  // 获取项目详情
  getProject: (id: string): Promise<ProjectResponse> => {
    return get<ProjectResponse>(`/projects/${id}`)
  },

  // 创建项目
  createProject: (data: ProjectRequest): Promise<ProjectResponse> => {
    return post<ProjectResponse>('/projects', data)
  },

  // 更新项目
  updateProject: (id: string, data: Partial<ProjectRequest>): Promise<ProjectResponse> => {
    return put<ProjectResponse>(`/projects/${id}`, data)
  },

  // 删除项目
  deleteProject: (id: string): Promise<any> => {
    return del(`/projects/${id}`)
  },

  // 发布项目
  publishProject: (id: string): Promise<ProjectResponse> => {
    return post<ProjectResponse>(`/projects/${id}/publish`)
  },

  // 关闭项目
  closeProject: (id: string): Promise<ProjectResponse> => {
    return post<ProjectResponse>(`/projects/${id}/close`)
  },

  // 搜索项目
  searchProjects: (params: QueryParams): Promise<PaginatedResponse<ProjectResponse['data']>> => {
    return get<PaginatedResponse<ProjectResponse['data']>>('/projects/search', params)
  },

  // 获取我的项目
  getMyProjects: (params?: QueryParams): Promise<PaginatedResponse<ProjectResponse['data']>> => {
    return get<PaginatedResponse<ProjectResponse['data']>>('/projects/my', params)
  },

  // 项目统计
  getProjectStats: (): Promise<any> => {
    return get('/projects/stats')
  },

  // 导出项目数据
  exportProjects: (params?: QueryParams): Promise<void> => {
    return get('/projects/export', params, { responseType: 'blob' })
  }
}

// 申请相关API
export const applicationApi = {
  // 获取申请列表
  getApplications: (params?: QueryParams): Promise<PaginatedResponse<ApplicationResponse['data']>> => {
    return get<PaginatedResponse<ApplicationResponse['data']>>('/applications', params)
  },

  // 获取申请详情
  getApplication: (id: string): Promise<ApplicationResponse> => {
    return get<ApplicationResponse>(`/applications/${id}`)
  },

  // 创建申请
  createApplication: (data: ApplicationRequest): Promise<ApplicationResponse> => {
    return post<ApplicationResponse>('/applications', data)
  },

  // 更新申请
  updateApplication: (id: string, data: Partial<ApplicationRequest>): Promise<ApplicationResponse> => {
    return put<ApplicationResponse>(`/applications/${id}`, data)
  },

  // 删除申请
  deleteApplication: (id: string): Promise<any> => {
    return del(`/applications/${id}`)
  },

  // 撤回申请
  withdrawApplication: (id: string): Promise<ApplicationResponse> => {
    return post<ApplicationResponse>(`/applications/${id}/withdraw`)
  },

  // 审核申请
  reviewApplication: (id: string, data: { status: string; comment?: string }): Promise<ApplicationResponse> => {
    return post<ApplicationResponse>(`/applications/${id}/review`, data)
  },

  // 获取我的申请
  getMyApplications: (params?: QueryParams): Promise<PaginatedResponse<ApplicationResponse['data']>> => {
    return get<PaginatedResponse<ApplicationResponse['data']>>('/applications/my', params)
  },

  // 申请统计
  getApplicationStats: (): Promise<any> => {
    return get('/applications/stats')
  }
}

// 文件上传API
export const uploadApi = {
  // 上传文件
  uploadFile: (file: File, onProgress?: (progress: number) => void): Promise<any> => {
    return upload('/upload/file', file, {
      onUploadProgress: (progressEvent) => {
        if (onProgress && progressEvent.total) {
          const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total)
          onProgress(progress)
        }
      }
    })
  },

  // 上传图片
  uploadImage: (file: File, onProgress?: (progress: number) => void): Promise<any> => {
    return upload('/upload/image', file, {
      onUploadProgress: (progressEvent) => {
        if (onProgress && progressEvent.total) {
          const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total)
          onProgress(progress)
        }
      }
    })
  },

  // 上传文档
  uploadDocument: (file: File, onProgress?: (progress: number) => void): Promise<any> => {
    return upload('/upload/document', file, {
      onUploadProgress: (progressEvent) => {
        if (onProgress && progressEvent.total) {
          const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total)
          onProgress(progress)
        }
      }
    })
  }
}
